package com.yc.jpyy.appeal.controller;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.common.utils.manydatasource.HandlerDataSource;
import com.yc.jpyy.appeal.dao.AppealDao;
import com.yc.jpyy.appeal.service.AppealService;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.utils.Tools;


@Controller
@RequestMapping(value = "/appeal")
@Scope("prototype")
public class AppealController extends BaseController{
	private static Logger logger = LoggerFactory.getLogger(AppealController.class);

	@Autowired
	private AppealService appealService;

	@Autowired
	private AppealDao appealDao;
	
	/**
	 * 新增信访案件管理
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/addAppeal", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String addAppeal(HttpServletRequest request) throws UnsupportedEncodingException {

		Map<String,Object> resultMap = new HashMap<>();
		request.setCharacterEncoding("UTF-8");
		try {
			/*InputStream is = request.getInputStream();  
			byte[] bytes = new byte[1024 * 1024]; 
			int nRead = 1;  
			int nTotalRead = 0;  
			while (nRead > 0) {  
			    nRead = is.read(bytes, nTotalRead, bytes.length - nTotalRead);  
			    if (nRead > 0)  
			        nTotalRead = nTotalRead + nRead;  
			} 
			String info = new String(bytes, 0, nTotalRead, "utf-8");*/ 
			String info = request.getAttribute("info").toString();
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = appealService.addAppeal(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("addAppeal" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "添加失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
	}
	
	/**
	 * 分页查询信访案件管理
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/queryAppealPage", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String queryAppealPage(HttpServletRequest request) throws UnsupportedEncodingException {

		Map<String,Object> resultMap = new HashMap<>();
		request.setCharacterEncoding("UTF-8");
		try {	
			String info = request.getParameter("info"); 
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = appealService.queryAppealPage(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("queryAppealPage" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "查询失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
	}
	
	
	/**
	 * 根据Id查询信访案件管理
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/queryAppealById", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String queryAppealById(HttpServletRequest request) throws UnsupportedEncodingException {

		Map<String,Object> resultMap = new HashMap<>();
		request.setCharacterEncoding("UTF-8");
		try {	
			String info = request.getParameter("info"); 
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = appealService.queryAppealById(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("queryAppealById" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "查询失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
	}
	
	
	/**
	 * 更新信访案件管理
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/updateAppeal", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String updateAppeal(HttpServletRequest request) throws UnsupportedEncodingException {

		Map<String,Object> resultMap = new HashMap<>();
		request.setCharacterEncoding("UTF-8");
		try {
			/*InputStream is = request.getInputStream();  
			byte[] bytes = new byte[1024 * 1024]; 
			int nRead = 1;  
			int nTotalRead = 0;  
			while (nRead > 0) {  
			    nRead = is.read(bytes, nTotalRead, bytes.length - nTotalRead);  
			    if (nRead > 0)  
			        nTotalRead = nTotalRead + nRead;  
			} 
			String info = new String(bytes, 0, nTotalRead, "utf-8");  */
			String info = request.getAttribute("info").toString();
			Map<String,Object> paramsMap = Tools.JsonToMap(info);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = appealService.updateAppeal(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("updateAppeal" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "更新失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
	}
	
}
